#include <iostream>
#include <cstdlib>
#include <boost/math/special_functions/binomial.hpp>
#include <cmath>

#include "Object.hpp"

// sum(k,n, C(n,i)*Pn(X=i)) with Pn(X=i)=p^i*q^(n-i)=p^i*(1-p)^(n-i)
long double proba(unsigned int n, unsigned int k, long double p = 0.5f);


int main(int argc, char** argv){
  unsigned int n,k, cycle;
  float p;
  while(1){
    std::cout << "n : ";
    std::cin >> n;
    std::cout << "k : ";
    std::cin >> k;
    std::cout << "p : ";
    std::cin >> p;
    std::cout << "cycle : ";
    std::cin >> cycle;
    std::cout << "result : " <<  powl(proba(n, k, p),(long double)cycle) << std::endl << std::endl;
  }
  return EXIT_SUCCESS;
}

long double proba(unsigned int n, unsigned int k, long double p){
  long double result =0.0f;
  
  if(p > 1.0f)
    p = 1.0f;
  if(p < 0.0f)
    p = 0.0f;

  for(unsigned int i=k; i <= n; i++){ // combination with i attacks 
    result += 
      powl(p, (long double)i) *powl(1.0f-p, (long double)(n-i)) * // Probability of each 
      boost::math::binomial_coefficient<long double>((long double)n,(long double)i); //
  }

  if(1.0f < result)
    return 1.0f;
  return result;
}
